package Thread_;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock:可重入锁
 */
public class Thread25 {
    /**
     * 让t1 slepp一下，t2线程先获取到锁，
     * t1用tryLock方法上锁，未获取到锁，返回false,不会阻塞
     * @param args
     */
    public static void main1(String[] args) {
        ReentrantLock reentrantLock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            System.out.println("t1 "+reentrantLock.tryLock());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("Thread 01");
            reentrantLock.unlock();
        });
        Thread t2 = new Thread(() -> {
            System.out.println("t2 "+reentrantLock.tryLock());
            try {
                Thread.sleep(1000);//
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("Thread 02");
            reentrantLock.unlock();
        });
        t1.start();
        t2.start();
        System.out.println("main ");
    }
}
